<?php

class bpBeer {

// Validate section ========================================	
	
	//Функиця проверяет введенный id. Параметр $isSet указывает на необходимость проверки существования $id в базе. $isSet = true - необходимо что бы существовала запись в базе с указанным айди, иначе только правильный тип переменной
	function validateFirmId($id, $isSet = false) {
		if (is_numeric($id) && !empty($id)) {
			if ($isSet) {
				$result = mysql_query("SELECT alias FROM beer_firm WHERE id='$id'");
				if (mysql_num_rows($result)) {
					return mysql_result($result, 0, "alias");
				} else {
					return false;
				}
			} else {
				return true;
			}
		} else {
			return false;
		}
	}
	
	function validateFirmRang($rang) {
		if (is_numeric($rang) && $rang>0 && $rang <= 1) {
			return true;
		} else {
			return false;
		}
	}
	
	function validateFirmAlias($alias, $isSet = false) {
		if (preg_match("/[a-zA-Z0-9]{1,50}/", $alias)) {
			if ($isSet) {
				$result = mysql_query(sprintf("SELECT id FROM beer_firm WHERE alias='%s'", mysql_real_escape_string($alias)));
				if (mysql_num_rows($result)) {
					return mysql_result($result, 0, "id");
				} else {
					return false;
				}
			} else {
			 return true;
			}
		} else {
			return false;
		}
	}


	
    function validateBeerId($id, $isSet = false) {
    if (is_numeric($id) && !empty($id)) {
            if ($isSet) {
                $result = mysql_query("SELECT alias FROM beer_firm_mark WHERE id='$id'");
                if (mysql_num_rows($result)) {
                    return mysql_result($result, 0, "alias");
                } else {
                    return false;
                }
            } else {
                return true;
            }
        } else {
            return false;
        }
    }
    
    function validateBeerAlias($alias, $isSet = false) {
        if (preg_match("/[a-zA-Z0-9]{1,50}/", $alias)) {
            if ($isSet) {
                $result = mysql_query(sprintf("SELECT id FROM beer_firm_mark WHERE alias='%s'", mysql_real_escape_string($alias)));
                if (mysql_num_rows($result)) {
                    return mysql_result($result, 0, "id");
                } else {
                    return false;
                }
            } else {
             return true;
            }
        } else {
            return false;
        }
    }
// end Validate section ========================================

	
	

// Get Section ========================================	
	function getFirmArray($id = 0, $byAlias = false) {
		if ($id == 0 || ($byAlias?bpBeer::validateFirmAlias($id):bpBeer::validateFirmId($id))) {
			$where = array();
			if ($id) {
				$where[] = ($byAlias?"alias":"id")."='$id'";
			}
			$where[] = "visible='1'";
			$where = implode(" AND ", $where);
			
			$result = mysql_query("SELECT * FROM beer_firm WHERE ".$where);
			$output = array();
			if ($id) {
    			$output = mysql_fetch_assoc($result);
			} else {
			 while ($row = mysql_fetch_assoc($result)) {
                    $row["name"] = htmlspecialchars($row["name"]);
                    //$row["descr"] = htmlspecialchars($row["descr"]);
                    $row["image_"] = '<img src="'.PATH_TO_IMAGES_BEER_FIRM.$row["image"].'" alt="'.$row["name"].'" title="'.$row["name"].'" />';
                    $output[] = $row;
                }
			}
			return $output;
		} else {
			return false;
		}
	}
	
	function getFirmRang($id) {
		if (bpBeer::validateFirmId($id)) {
			$result = mysql_query("SELECT rang FROM beer_firm WHERE id='$id'");
			return mysql_result($result, 0, "rang");
		} else {
			return false;
		}
	}
	
	function getBeer($sFirm, $sBeer) {
		$query = "SELECT bf.id as fid,
				bf.name as fname,
				bf.alias as falias,
				bf.descr as fdecr,
				bf.content as fcontent,
				bf.image as fimage,
				bfm.id as bid,
				bfm.name as bname,
				bfm.content as bcontent,
				bfm.image as bimage
			FROM beer_firm bf INNER JOIN beer_firm_mark bfm ON bfm.firm_id=bf.id WHERE bf.alias='$sFirm' AND bfm.alias='$sBeer' AND bf.visible AND bfm.visible";
		$result = mysql_query($query);
		return mysql_fetch_assoc($result);
	}
	
	function getBeerByFirm($id) {
		if (bpBeer::validateFirmId($id)) {
			$sQuery = "SELECT b.id, b.firm_id, b.name, b.alias, b.descr, b.content, b.image, b.rang, s.name as sortname  FROM beer_firm_mark b INNER JOIN beer_sorts s ON s.id=b.beer_sort_id  WHERE firm_id='$id'";
			$result = mysql_query($sQuery);
			$output = array();
			while ($row = mysql_fetch_assoc($result)) {
				$row["name"] = htmlspecialchars($row["name"]);
				$row["sortname"] = htmlspecialchars($row["sortname"]);
				$row["image_"] = '<img src="'.PATH_TO_IMAGES_BEER_BEER.$row["image"].'" alt="'.$row["name"].'" title="'.$row["name"].'" />';
				$output[$row["id"]] = $row;
			}
			return $output;
		} else {
			return false;
		}
	}
	

// end Get Section ========================================	
	
// Add Section ========================================	
	
	function addFirmRang($id, $rang = 0.1) {
		if (bpBeer::validateFirmId($id) && bpBeer::validateFirmRang($rang)) {
			$result = mysql_query("SELECT rang FROM beer_firm WHERE id='$id'");
			$newRang = $rang + mysql_result($result, 0, "rang");
			if (mtsql_query("UPDATE beer_firm SET rang='$newRang'")) {
			    return $newRang;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}
	
	function addBeerRang($id, $rang = 0.1) {
		if (bpBeer::validateBeerId($id) && bpBeer::validateBeerRang($rang)) {
		    $result = mysql_query("SELECT rang FROM beer_firm_mark WHERE id='$id'");
		    $newRang = $rang + mysql_result($result, 0, "rang");
		    if (mtsql_query("UPDATE beer_firm_mark SET rang='$newRang'")) {
			return $newRang;
		    } else {
			return false;
		    }
		} else {
		    return false;
		}
	}
	
//end Add Section ========================================	
}



?>